package de.reondo.jos.filters;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


public final class PackagePrefixFilter implements IFilter<Class<?>> {

	private List<String> allowedPackagePrefixes;
	
	public PackagePrefixFilter(String packagePrefix) {
		this.allowedPackagePrefixes = Collections.singletonList(packagePrefix);
	}

	public PackagePrefixFilter(List<String> packagePrefixes) {
		this.allowedPackagePrefixes = new ArrayList<String>(packagePrefixes);
	}
	
	@Override
	public boolean accept(Class<?> cls) {
		if (cls == null || cls.getPackage() == null) {
			// primitive types have no package
			return false;
		}
		String pkgName = cls.getPackage().getName();
		for (String prefix : allowedPackagePrefixes) {
			if (pkgName.startsWith(prefix)) {
				return true;
			}
		}
		return false;
	}

}